package org.stvd.repository.admin.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.persistence.Query;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.entities.admin.Dictionary;
import org.stvd.repository.admin.DictionaryDao;
import org.stvd.repository.base.impl.BaseDaoImpl;

/**
 * @Title: DictionaryDaoImpl
 * @Description: 系统字典-Repository接口实现类
 * @author houzx
 * @date 2020年2月7日
 */
@Repository("DictionaryDao")
public class DictionaryDaoImpl extends BaseDaoImpl<Dictionary> implements DictionaryDao {
    
    @Override
    @Transactional
    public void updateDictionaryType(String oldType, String newType) {
        String sql = "update dictionary SET type = :newType WHERE type = :oldType";
        Query query = em.createNativeQuery(sql);
        query.setParameter("newType", newType);
        query.setParameter("oldType", oldType);
        query.executeUpdate();
        em.flush();
    }
    
    @Override
    @Transactional
    public void updateDictionaryUpperId(String oldId, String newId) {
        String sql = "update dictionary SET upper_id = :newId WHERE upper_id = :oldId";
        Query query = em.createNativeQuery(sql);
        query.setParameter("oldId", oldId);
        query.setParameter("newId", newId);
        query.executeUpdate();
        em.flush();
    }
    
    @Override
    @Transactional
    public void deleteDictionary(String typeId, String dicId) {
        String sql = "DELETE FROM dictionary WHERE type = :typeId AND id = :dicId";
        Query query = em.createNativeQuery(sql);
        query.setParameter("typeId", typeId);
        query.setParameter("dicId", dicId);
        query.executeUpdate();
        em.flush();
    }

    @Override
    public Dictionary findByTypeID(String type, String id) {
        String hql = " FROM Dictionary where type = ?0 AND id = ?1";
        List<Dictionary> list = findByHQL(hql, type, id);
        if(list!=null && list.size() > 0) {
            return list.get(0);
        }
        return null;
    }
    
    @Override
    public List<Dictionary> listDictionaryByTypeUpperId(String type, String upperId) {
        String hql = " FROM Dictionary where type = ?0 AND upperId = ?1 ORDER BY serialNo";
        return findByHQL(hql, type, upperId);
    }
    
	@Override
	public List<Dictionary> listDictionaryByType(String type) {
		String hql = " FROM Dictionary where type = ?0 ORDER BY serialNo";
		return findByHQL(hql, type);
	}

	@Override
	public List<Map<String, Object>> listAllDicsByType(String type) {
		String sql = " SELECT d.id, d.serial_no, "
				+ " CASE "
					+ " WHEN INSTR(d.serial_no,'0') = 2 then d.content"
					+ " WHEN INSTR(d.serial_no,'0') = 3 then CONCAT('　　',d.content)"
					+ " WHEN INSTR(d.serial_no,'0') = 0 then CONCAT('　　　　',d.content)"
				+ " END AS content "
				+ " FROM dictionary d "
				+ " WHERE d.type = :type "
				+ " ORDER BY d.serial_no";
		Map<String, Object> params = new HashMap<>();
		params.put("type", type);
		return findBySQLToMap(0, 0, sql, params);
	}

	@Override
	public List<Dictionary> findAllUpperDicsByType(String type) {
		String hql = " FROM Dictionary d where d.id in ("
				+ " SELECT distinct t.upperId FROM Dictionary t "
				+ " WHERE t.upperId != '-1' AND type = ?0 ) "
			+ " ORDER BY d.serialNo ";
		return findByHQL(hql, type);
	}
}
